﻿using OfficeOpenXml;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows.Shapes;
using System.Xml.Serialization;

namespace IP20.Procdure
{
    public class Config
    {
        private static string file = "config.xlsx";

        static Config()
        {
#if JQ8931314
            file = "jq89313_jq89414_config.xlsx";
#endif
        }

        public static Dictionary<string, string> Params { get; set; }

        private static void LoadTestConfig(ExcelWorksheet ws)
        {
            Params = new Dictionary<string, string>();
            int row = 1;
            while (true)
            {
                ExcelRangeBase cell1 = ws.Cells[row, 1];
                ExcelRangeBase cell2 = ws.Cells[row, 2];
                if (cell1.Value == null)
                {
                    break;
                }
                if (string.IsNullOrEmpty(cell1.Value.ToString().Trim(' ')))
                {
                    break;
                }
                string key = cell1.Value.ToString().Trim(' ');

                string value = cell2.Value.ToString().Trim(' ');

                Params.Add(key, value);
                row++;
            }
        }

        public static void ParseExcelFile()
        {
            string path = file;
            ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
            if (!File.Exists(path))
            {
                throw new FileNotFoundException(path);
            }
            using (ExcelPackage package = new ExcelPackage(path))
            {
                ExcelWorksheet wsParam = package.Workbook.Worksheets["配置"];

                LoadTestConfig(wsParam);
            }
        }

        public static void SaveParam()
        {
            ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
            if (!File.Exists(file))
            {
                throw new FileNotFoundException(file);
            }
            using (ExcelPackage package = new ExcelPackage(file))
            {
                ExcelWorksheet wsParam = package.Workbook.Worksheets["配置"];

                // 遍历Params字典，更新Excel中的值
                foreach (var item in Params)
                {
                    // 在第一列中查找匹配item.Key的单元格
                    var keyCell = wsParam.Cells["A:A"].FirstOrDefault(c => c.Value?.ToString() == item.Key);

                    if (keyCell != null)
                    {
                        // 获取同一行下一列的单元格（B列）
                        int row = keyCell.Start.Row;
                        wsParam.Cells[row, 2].Value = item.Value; // B列写入值
                    }
                    else
                    {
                        // 如果找不到匹配项，可以选择在最后一行添加新记录
                        int lastRow = wsParam.Dimension?.End.Row ?? 1;
                        wsParam.Cells[lastRow + 1, 1].Value = item.Key;   // A列写入Key
                        wsParam.Cells[lastRow + 1, 2].Value = item.Value; // B列写入Value
                    }
                }

                // 保存修改
                package.Save();
            }
        }
    }
}